<?php
/**
 * install.php - database installation file
 *
 * NMIT BIT Project - Infinity - A 3D Website using WebGL & HTML5 Technology
 * Infinity Grid Version 
 * Version: 1.0 Beta 2
 *
 * Infinity - Login function test
 *
 * Author: William Liu <qzliuyuzou@gmail.com> <lyzdev@gmail.com>
 *
 * Required Server Version:
 *  PHP5 or higher
 *  MySQL 5 or higher
 *
 * Required Browser Version:
 *  Google Chrome 9 or higher
 *  Mozilla Firefox 4 Beta or higher
 *  *Apple Safari
 *
 * Copyright (c) 2011, William Liu All rights reserved.
 * Infinity is an open source 3D website project
 * Under Apache License 2.0
 * http://www.lyzsoft.cn
 * http://7cloud.wordpress.com
 *
 * Project Starting date: 9/3/2011
 *
**/
?> 

<?php
$files="config.php";

if(!is_writable($files))
{
	echo "<font color=red>Configration file is NOT writeable!</font>"; 
	}
else
{
	echo "<font color=green>Configration file is writeable!</font>"; 
	}

if(isset($_POST[install]))
{
	$config_str = "<?php";
	$config_str .= "\n";
	$config_str .= '$db_host = "' . $_POST[db_host] . '";';
	$config_str .= "\n";
	$config_str .= '$db_user = "' . $_POST[db_user] . '";';
	$config_str .= "\n";
	$config_str .= '$db_pass = "' . $_POST[db_pass] . '";';
	$config_str .= "\n";
	$config_str .= '$db_name = "' . $_POST[db_name] . '";';
	$config_str .= "\n";
	$config_str .= '$db_prefix = "' . $_POST[db_tag] . '";';
	$config_str .= "\n";
	$config_str .= '?>';
	
	$fileOpen = fopen($files, "w+"); 
	fwrite($fileOpen, $config_str); 
	
//===================== 

include_once ("config.php"); 

if (!@$link = mysql_connect($db_host, $db_user, $db_pass)) 
{
	//check db connection
	echo "Cannot cannect to database, please check your database setting <a href=install.php><span><- Fix it!</span></a>"; 
	}
else
{
	mysql_query("CREATE DATABASE `$db_name`");
	 
	mysql_select_db($db_name);
	
	// msg table
	$sql_query[] = "CREATE TABLE `" . $db_prefix . "msg` ( 
	`id` INT( 11 ) NOT NULL AUTO_INCREMENT ,
	`avatarID` INT( 11 ) NOT NULL DEFAULT '0',
	`content` VARCHAR( 200 ) NOT NULL ,
	`addtime` INT( 10 ) NOT NULL ,
	PRIMARY KEY ( `id` )
	)ENGINE = MYISAM DEFAULT CHARSET = utf8;";
	
	// users table
	$sql_query[] = "CREATE TABLE `" . $db_prefix . "users` ( 
	`ID` INT( 6 ) NOT NULL AUTO_INCREMENT ,
	`username` VARCHAR( 9 ) NOT NULL ,
	`password` VARCHAR( 9 ) NOT NULL ,
	`user_level` TINYINT( 1 ) NOT NULL DEFAULT '1',
	`user_enable` TINYINT( 1 ) NOT NULL DEFAULT '1',
	PRIMARY KEY ( `ID` )
	) ENGINE = MYISAM DEFAULT CHARSET = utf8;";
	
	// Add defult administrator
	$sql_query[] = "insert into users (username, password, user_level) VALUES ('admin', 'P@ssword1', '5')";

	foreach($sql_query as $val)
	{
		mysql_query($val); 
		} 
		echo "<script>alert('Website Installed success!\nDefult Admin is \"admin\" \"P@ssword1\"'); location.href='index.php'</script>"; 
	
		//Rename install.php for security reason
		rename("install.php","install.lock");
		}
} 
?> 



<!DOCTYPE html>
<head>  
<meta charset="utf-8">
<title>Infinity - Register</title>
<link rel="stylesheet" type="text/css" href="../style.css"/>
</head>

<body>  

<form id="install" name="install" method="post" action="">  
  <table width="300" border="0" align="center" cellpadding="2" cellspacing="2">  
    <tr>  
      <td width="150"><div align="left">Host:</div></td>  
      <td width="150"><input type="text" name="db_host" value="localhost"></td>  
    </tr>  
    <tr>  
      <td><div align="left">Database User</div></td>  
      <td><input type="text" name="db_user" value="root"></td>  
    </tr>
    <tr>  
      <td><div align="left">Database PW</div></td>  
      <td><input type="text" name="db_pass"></td>  
    </tr>
    <tr>  
      <td><div align="left">Database Name</div></td>  
      <td><input type="text" name="db_name" value="infinity"></td>  
    </tr>
    <tr>  
      <td><div align="left">Database Prefix</div></td>  
      <td><input type="text" name="db_prefix" value="inf_"></td>  
    </tr>
  </table>  
  <p align="center">  
    <button type=submit name=install>Next</button>  
  </p>
</form>

</body>  
</html>  